package com.zzh.takeoutapp.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.zzh.takeoutapp.R;
import com.zzh.takeoutapp.model.HomeBean;
import com.zzh.takeoutapp.utils.ImageManager;

import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 *
 */
public class TypeHistoryAdapter extends RecyclerView.Adapter<TypeHistoryAdapter.TypeHistoryHolder> {

    private Context mContext;


    private List<HomeBean.DataBean.CateAdvBean>  mHomehopspot;

    private LayoutInflater inflater;


    public TypeHistoryAdapter(Context mContext, List<HomeBean.DataBean.CateAdvBean> cateAdvBeans) {
        this.mContext = mContext;
        this.mHomehopspot = cateAdvBeans;
        inflater = LayoutInflater.from(mContext);

    }

    @Override
    public TypeHistoryHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new TypeHistoryHolder(inflater.inflate(R.layout.itam_homepageradapter_rv2, parent, false));
    }

    @Override
    public void onBindViewHolder(final TypeHistoryHolder holder, final int position) {
        HomeBean.DataBean.CateAdvBean cateAdvBean= mHomehopspot.get(position);
        ImageManager.loadUrlImage(mContext,cateAdvBean.getThumb(),holder.homeReadPivIv);
        holder.homeReadTitle.setText(cateAdvBean.getTitle());
        if (mOnItemClickLitener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnItemClickLitener.onItemClick(holder.itemView, position);
                }
            });
            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    mOnItemClickLitener.onItemLongClick(holder.itemView, position);
                    return true;
                }
            });
        }

    }
    public interface OnItemClickListener {
        void onItemClick(View v, int postion);

        void onItemLongClick(View v, int postion);
    }

    @Override
    public int getItemCount() {
        return mHomehopspot == null ? 0 : mHomehopspot.size();
    }

    public class TypeHistoryHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.home_read_piv_iv)
        ImageView homeReadPivIv;
        @Bind(R.id.home_read_title)
        TextView homeReadTitle;


        public TypeHistoryHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

    /**
     * 自定义条目点击监听
     */
    private OnItemClickListener mOnItemClickLitener;

    public void setmOnItemClickLitener(OnItemClickListener mOnItemClickLitener) {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }

}
